﻿package cn.antscript.airCMS.display.component.btnbase 
{
	import cn.antscript.airCMS.data.singleData.AbstractData;
	import cn.antscript.airCMS.data.singleData.TextData;
	import cn.antscript.airCMS.display.component.EditPanel;
	import cn.antscript.airCMS.display.component.SurePanel;
	import cn.antscript.airCMS.display.component.WaitBar;
	import cn.antscript.model.SqlModel;
	import flash.display.Bitmap;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author antscript
	 */
	public class TextBtn extends Sprite
	{
		private static var _format:TextFormat = new TextFormat(null, 12, 0x999999);
		
		private var _editBtn:AbstractIco;
		private var _deleteBtn:AbstractIco;
		
		private var _bitmap:Bitmap;
		protected var _txt:TextField;
		
		public var data:TextData;
		
		public function TextBtn(data_:AbstractData = null,delEnabled:Boolean=true,container_:DisplayObjectContainer=null, xPos:int = 0, yPos:int = 0) 
		{
			data = data_ as TextData;
			
			if (container_ != null) {
				container_.addChild(this);
			}
			this.x = xPos;
			this.y = yPos;
			
			_bitmap = new Bitmap(IcoBitmapdatas.SmallText);
			addChild(_bitmap);
			
			
			_txt = new TextField();
			_txt.x = 30;
			_txt.y = 3;
			_txt.width = 550;
			_txt.height = 30;
			_txt.text = data.label;
			_txt.setTextFormat(_format);
			_txt.selectable = false;
			addChild(_txt);
			
			
			_editBtn = new AbstractIco(IcoBitmapdatas.Edit, "编辑", this, 600, 3);
			//if (delEnabled) {
				_deleteBtn = new AbstractIco(IcoBitmapdatas.Delete, "删除",this,640,3);
			//}
			
			this.buttonMode = true;
			
			this.addEventListener(MouseEvent.CLICK, onClick);			
			this.addEventListener(MouseEvent.ROLL_OVER, onOver);
			this.addEventListener(MouseEvent.ROLL_OUT, onOut);
			
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemove);
		}		
		
		private function onClick(e:MouseEvent):void 
		{
			if (e.target == _deleteBtn) {
				SurePanel.getInstance().show("提示：一经删除则不可恢复，确定删除？",this.stage,del);
			}
			else {
				EditPanel.getInstance().edit(data, this.stage);
			}
			
		}
		
		private function del():void
		{
			WaitBar.getInstance().show(this.stage);
			SqlModel.getInstance().del(data,delCom);
		}
		
		private function delCom():void
		{
			WaitBar.getInstance().hide();
			dispatchEvent(new Event("deleted"));
		}
		
		private function onRemove(e:Event):void 
		{
			this.removeEventListener(Event.REMOVED_FROM_STAGE, onRemove);
			
			this.removeEventListener(MouseEvent.CLICK, onClick);		
			this.removeEventListener(MouseEvent.ROLL_OVER, onOver);
			this.removeEventListener(MouseEvent.ROLL_OUT, onOut);
			
			_bitmap.bitmapData = null;
			data = null;
		}
		private function onOver(e:MouseEvent):void 
		{	
			
			
			graphics.clear();
			graphics.lineStyle(2, 0xffffff);
			graphics.beginFill(0x000000, 1);
			graphics.drawRect(0, 0, 30+_txt.width, 25);
			graphics.endFill();
			
			if (_txt.width < _txt.textWidth) {
				addEventListener(Event.ENTER_FRAME, onEnter);
			}				
			
			
		}		
		private function onEnter(e:Event):void
		{
			
			_txt.scrollH += 1;
			if (_txt.scrollH == _txt.maxScrollH) {
				removeEventListener(Event.ENTER_FRAME, onEnter);
			}
		}
		private function onOut(e:MouseEvent):void 
		{
			graphics.clear();
			_txt.scrollH = 0;			
			removeEventListener(Event.ENTER_FRAME, onEnter);
		}
		
	}

}